package com.eduhzy.ttw.teacher.mvp.presenter;

import android.app.Application;
import android.os.Message;

import com.blankj.utilcode.util.ObjectUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.ChooseSubjectData;
import com.eduhzy.ttw.commonsdk.entity.CommonData;
import com.eduhzy.ttw.commonsdk.holder.AutoBaseViewHolder;
import com.eduhzy.ttw.teacher.mvp.contract.ChooseSubjectContract;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.RxLifecycleUtils;

import org.simple.eventbus.EventBus;

import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;
import me.jessyan.rxerrorhandler.handler.RetryWithDelay;


@ActivityScope
public class ChooseSubjectPresenter extends BasePresenter<ChooseSubjectContract.Model, ChooseSubjectContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;
    @Inject
    BaseQuickAdapter<ChooseSubjectData, AutoBaseViewHolder> mAdapter;

    @Inject
    public ChooseSubjectPresenter(ChooseSubjectContract.Model model, ChooseSubjectContract.View rootView) {
        super(model, rootView);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }

    public void getSubject(String classId, String userId) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("classId", classId);
        map.put("userId", userId);
        mModel.getSubject(map)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(2, 3))
                .doOnSubscribe(disposable -> {
                    //显示下拉刷新的进度条
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    //隐藏下拉刷新的进度条
                    mRootView.hideLoading();
                })
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<ChooseSubjectData[]>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<ChooseSubjectData[]> commonData) {
                        if (!commonData.isResult()) {
                            mRootView.showMessage(commonData.getMessage());
                        } else {
                            ChooseSubjectData[] data = commonData.getData();
                            List<ChooseSubjectData> mLists = Arrays.asList(data);
                            mAdapter.getData().clear();
                            mAdapter.addData(mLists);
                            if (ObjectUtils.isNotEmpty(mLists) && mAdapter.getFooterLayoutCount() <= 0) {
                                mRootView.addFooter();
                            }
                        }
                    }
                });
    }

    public void updateTeaSubject(String teacherId, String classId, String userId, String ids) {
        Map<String, Object> map = new LinkedHashMap<>();
        map.put("teacherId", teacherId);
        map.put("classId", classId);
        map.put("userId", userId);
        map.put("subjectIds", ids);

        mModel.updateTeaSubject(map)
                .subscribeOn(Schedulers.io())
                .retryWhen(new RetryWithDelay(2, 3))
                .doOnSubscribe(disposable -> {
                    //显示下拉刷新的进度条
                    mRootView.showLoading();
                }).subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .doFinally(() -> {
                    //隐藏下拉刷新的进度条
                    mRootView.hideLoading();
                })
                //使用 Rxlifecycle,使 Disposable 和 Activity 一起销毁
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ErrorHandleSubscriber<CommonData<Map>>(mErrorHandler) {
                    @Override
                    public void onNext(CommonData<Map> commonData) {
                        if (commonData.isResult()) {
                            Message obtain = Message.obtain();
                            obtain.what = EventBusTags.CHOOSE_SUBJECT_SUCCESS;
                            EventBus.getDefault().post(obtain, RouterHub.APP_CLASSTEACHERACTIVITY);
                            mRootView.killMyself();
                        }
                    }
                });
    }
}
